"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
Object.defineProperty(exports, "__esModule", { value: true });
const users_1 = require("../model/users");
const config_1 = require("../config/config");
const jwt = require("jsonwebtoken");
function default_1(app) {
    // 登录接口
    app.post("/api/login", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const user = req.body;
            const { success, data } = yield (0, users_1.readLoiginData)(user);
            if (!success) {
                res.json({ success: false, msg: "用户名或密码错误" });
                return;
            }
            else if (data.length == 0) {
                res.json({ success: false, msg: "用户名或密码错误" });
                return;
            }
            /**
             * 获取token
             * jwt.sign({对象},密钥,有效期)
             */
            const token = jwt.sign({ id: data[0].id }, config_1.tokenKey, {
                expiresIn: 24 * 60 * 60,
            });
            res.json({ success: true, token: token, data: data });
        });
    });
    //注册接口
    app.post("/api/register", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const user = req.body;
            const { success, data } = yield (0, users_1.readRegisterData)(user);
            if (!success) {
                res.json({ success: false, msg: data.message });
                return;
            }
            else if (data.length == 0) {
                res.json({ success: false, msg: data.message });
                return;
            }
            res.json({ success: true, msg: "添加成功" });
        });
    });
    // 获取用户数据列表
    app.get("/api/getUserList", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const { page } = req.query;
            const { success, data } = yield (0, users_1.getUserList)(Number(page));
            if (!success) {
                res.json({ success: false, msg: data.message });
                return;
            }
            else if (data.length == 0) {
                res.json({ success: false, msg: data.message });
                return;
            }
            res.json({ success: true, data, total: data.length });
        });
    });
    // 删除用户
    app.get("/api/deluser", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const { id } = req.query;
            const { success, data } = yield (0, users_1.delUser)(id);
            if (!success) {
                res.json({ success: false, msg: data.message });
                return;
            }
            else if (data.length == 0) {
                res.json({ success: false, msg: data.message });
                return;
            }
            res.json({ success: true, msg: "删除成功" });
        });
    });
    // 更新用户
    app.post("/api/updateUser", function (req, res) {
        return __awaiter(this, void 0, void 0, function* () {
            const user = req.body;
            console.log(req.uids, "---s");
            // console.log(req);
            const { success, data } = yield (0, users_1.updateUser)(user);
            if (!success) {
                res.json({ success: false, msg: data.message });
                return;
            }
            else if (data.length == 0) {
                res.json({ success: false, msg: data.message });
                return;
            }
            res.json({ success: true, msg: "更新成功" });
        });
    });
}
exports.default = default_1;
